package com.hsh.tools.convert;

import com.hsh.entity.DbDepartment;
import com.hsh.service.IDbDepartmentService;
import com.hsh.tools.AcCollectionUtils;
import com.hsh.vo.result.convert.DeptVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class DeptConvert {

    @Autowired
    IDbDepartmentService departmentService;

    public static List<DeptVO> converterToMenuNodeVO(List<DbDepartment> vo) {
        List<DeptVO> deptVO = new ArrayList<>();
        if (AcCollectionUtils.isNotEmpty(vo)) {
            for (DbDepartment dept : vo) {
                DeptVO data = new DeptVO();
                BeanUtils.copyProperties(dept, data);
                deptVO.add(data);
            }
        }
        return deptVO;
    }

    // 第一级部门
    private final String SYS_JTDSH = "SYS_JTDSH";

    /**
     * 返回指定节点的最大父节点【第一级节点】对象信息
     * 返回公司其下的第一级部门对象信息
     */
    public DbDepartment getParentMenuByMenuId(String deptId) {
        // 找出所在的链
        DbDepartment department = departmentService.getById(deptId);
        if (Objects.nonNull(department)) {
            if (Objects.equals(SYS_JTDSH, department.getParentId())) {
                return department;
            }
            DbDepartment dept = departmentService.getById(department.getParentId());
            if (Objects.nonNull(dept)) {
                return getParentMenuByMenuId(dept.getDeptId());
            }
            return null;
        }
        return null;
    }
}
